#include "StorageInformation.h"
#include <QListWidget>
#include <QScrollArea>
#include <QResizeEvent>

GetStorageInfo::GetStorageInfo(QWidget *parent)
    : QMainWindow(parent)
{
    setWindowTitle("Get storage information");

    /*The sysInfo becomes a child of the GetDisplayInfo, and will be destroyed when the GetDisplayInfo is deleted */
    sysInfo = new QSystemStorageInfo(this);

    /* To show items in list view */
    listWidget = new QListWidget();
    listWidget->setObjectName(QString::fromUtf8("listWidget"));

    /* To make UI scrollable */
    scrollArea = new QScrollArea();
    setWidgetGeometry(&(size()));

    /* scrollArea takes ownership of the listWidget, so no need to delete it */
    scrollArea->setWidget(listWidget);
    scrollArea->setAlignment(Qt::AlignLeft);
    scrollArea->setWidgetResizable(true);

    /* QMainWindow takes ownership of the scrollArea, so no need to delete it */
    setCentralWidget(scrollArea);
    getStorageInformation();
}

GetStorageInfo::~GetStorageInfo()
{

}

void GetStorageInfo::resizeEvent (QResizeEvent* event)
{
    QSize widgetSize = event->size();
    setWidgetGeometry(&widgetSize);
    QMainWindow::resizeEvent(event);
}

void GetStorageInfo::setWidgetGeometry(QSize* screenSize)
{
    resize(*screenSize);
    QRect rect(0,0,screenSize->width(), screenSize->height());
    listWidget->setGeometry(rect);
}

void GetStorageInfo::getStorageInformation()
{
    QString strInfo;
        //* qlonglong defined in 4.2 same as qint64

    qlonglong 	 diskSpace;

    /*get logical drives */
    QStringList driveList = sysInfo->logicalDrives();

    strInfo.append("Drives information: ") ;
    listWidget->addItem(strInfo);

    foreach ( QString str, driveList )
    {
        strInfo.clear();
        strInfo.append("   Drive name: " + str);
        listWidget->addItem(strInfo);

        /* get available space on drive. */
        diskSpace = sysInfo->availableDiskSpace(str);

        strInfo.clear();

        if(diskSpace > 1000000)
        {
            diskSpace /= 1000000;
            strInfo.setNum(diskSpace);
            strInfo.prepend("   Available space: ");
            strInfo.append("MB");

        }
        else if(diskSpace > 1000)
        {
            diskSpace /= 1000;
            strInfo.setNum(diskSpace);
            strInfo.prepend("   Available space: ");
            strInfo.append("KB");

        }
        else
        {
            strInfo.setNum(diskSpace);
            strInfo.prepend("   Available space: ");
            strInfo.append("Bytes");
        }

        listWidget->addItem(strInfo);

        /* get total space on drive */
        diskSpace = sysInfo->totalDiskSpace(str);

        strInfo.clear();

        if(diskSpace > 1000000)
        {
            diskSpace /= 1000000;
            strInfo.setNum(diskSpace);
            strInfo.prepend("   Total space: ");
            strInfo.append("MB");

        }
        else if(diskSpace > 1000)
        {
            diskSpace /= 1000;
            strInfo.setNum(diskSpace);
            strInfo.prepend("   Total space: ");
            strInfo.append("KB");

        }
        else
        {
            strInfo.setNum(diskSpace);
            strInfo.prepend("   Total space: ");
            strInfo.append("Bytes");
        }

        listWidget->addItem(strInfo);

        strInfo.clear();
        strInfo.append("   Type of drives: ");

        /* get type of drive */
        switch(sysInfo->typeForDrive(str))
        {
        case QSystemStorageInfo::InternalDrive:
            {
            strInfo.append("InternalDrive");
            break;
            }
        case QSystemStorageInfo::RemovableDrive:
            {
            strInfo.append("RemovableDrive");
            break;
            }
        case QSystemStorageInfo::RemoteDrive:
            {
            strInfo.append("RemoteDrive");
            break;
            }
        case QSystemStorageInfo::CdromDrive:
            {
            strInfo.append("CdromDrive");
            break;
            }
        case QSystemStorageInfo::NoDrive:
        default:
            {
            strInfo.append("NoDrive available");
            break;
            }
        }

        listWidget->addItem(strInfo);

        strInfo.clear();
        strInfo.append("  ");
        listWidget->addItem(strInfo);
    }
}
